package com.neiquan.neiquandemo.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;

import org.haitao.common.utils.CommonCipher;


/**
 * SharedPreferencesUtil
 * 作者: hjb
 * 时间：2016/3/22.
 */
public class SPUtils {
    private static String APP_NAME = "neiquan";
    private static SharedPreferences sp;


    private SPUtils() {
    }

    public static SharedPreferences getSp() {
        if (sp == null) {
            sp = Tools.getContext().getSharedPreferences(APP_NAME, Context.MODE_PRIVATE);
        }
        return sp;
    }

    public static void saveStringToSP(String key, String valueStr) {
        if (!TextUtils.isEmpty(key)) {
            SharedPreferences sp = getSp();
            SharedPreferences.Editor editor = sp.edit();
            editor.putString(key, valueStr);
            //提交当前数据
            editor.commit();
        }
    }

    public static void saveBooleanToSP(String key, boolean defaultBoolean) {
        if (!TextUtils.isEmpty(key)) {
            SharedPreferences sp = getSp();
            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean(key, defaultBoolean);
            //提交当前数据
            editor.commit();
        }
    }

    public static boolean getBooleanSP(String key, boolean defaultBoolean) {
        SharedPreferences sp = getSp();
        if (!TextUtils.isEmpty(key)) {
            return sp.getBoolean(key, defaultBoolean);
        }
        return defaultBoolean;
    }

    public static String getStringSP(String key, String defaultValue) {
        SharedPreferences sp = getSp();
        if (!TextUtils.isEmpty(key)) {
            return sp.getString(key, defaultValue);
        }
        return null;
    }

    public static void saveBean(String key, Object o) {
        if (!TextUtils.isEmpty(key)) {
            if (o != null) {
                String s = JSON.toJSONString(o);
                saveByDes(key, CommonCipher.encrypt(CommonCipher.AES_KEY, s));
            }
        }
    }

    public static String getByDes(String key) {
        String content = null;
        if (!TextUtils.isEmpty(key)) {
            SharedPreferences sp = getSp();
            content = sp.getString(key, "");
        }
        return "".equals(content) ? null : CommonCipher.decrypt(CommonCipher.AES_KEY, content);
    }

    public static void saveByDes(String key, String content) {
        if (!TextUtils.isEmpty(key)) {
            if (content != null) {
                SharedPreferences sp = getSp();
                SharedPreferences.Editor editor = sp.edit();
                editor.putString(key, CommonCipher.encrypt(CommonCipher.AES_KEY, content));
                editor.commit();
            }
        }
    }
    public static void delete(Context context, String key) {
        SharedPreferences sp = getSp();
        SharedPreferences.Editor editor = sp.edit();
        editor.remove(key);
        editor.commit();
    }
    public static Object getBean(String key, Class<?> c) {
        if (!TextUtils.isEmpty(key)) {
            String s = getByDes(key);
            if (!TextUtils.isEmpty(s)) {
                return JSON.parseObject(CommonCipher.decrypt(CommonCipher.AES_KEY, s), c);
            }
        }
        return null;
    }
}
